package com.lishangshan.oas_system.controller.attendce;

import com.lishangshan.oas_system.model.dao.attendcedao.AttendceDao;
import com.lishangshan.oas_system.model.dao.attendcedao.AttendceService;
import com.lishangshan.oas_system.model.dao.system.StatusDao;
import com.lishangshan.oas_system.model.dao.system.TypeDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.dao.user.UserService;
import com.lishangshan.oas_system.model.entity.attendce.Attends;
import com.lishangshan.oas_system.utils.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class AttendcelistController {
    @Autowired
    AttendceDao attenceDao;
    @Autowired
    AttendceService attendceService;
    @Autowired
    UserDao uDao;
    @Autowired
    UserService userService;
    @Autowired
    TypeDao typeDao;
    @Autowired
    StatusDao statusDao;
    // 格式转化导入
    DefaultConversionService service = new DefaultConversionService();
    @Autowired
    private AttendceConFun attendceConFun;

    /**
     * 用戶 個人考勤 初始化
     *
     * @param request
     * @param model
     * @param session
     * @param page
     * @param baseKey
     * @param type
     * @param status
     * @param time
     * @param icon
     * @return
     */
    // 考情列表（当前登陆用户）
    @RequestMapping(value = "attendcelist")
    public String test(HttpServletRequest request, Model model, HttpSession session,
                       @RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page,
                       @RequestParam(value = "baseKey", required = false) String baseKey,
                       @RequestParam(value = "type", required = false) String type,
                       @RequestParam(value = "status", required = false) String status,
                       @RequestParam(value = "time", required = false) String time,
                       @RequestParam(value = "icon", required = false) String icon) {
        attendceConFun.signsortpaging(request, model, session, page, baseKey, type, status, time, icon);
        return "attendce/attendcelist";
    }

    /**
     * 分页 and 搜索 时 重新刷新 列表 数据 控制器
     *
     * @param request
     * @param model
     * @param session
     * @param page
     * @param baseKey
     * @param type
     * @param status
     * @param time
     * @param icon
     * @return
     */
    @RequestMapping(value = "attendcelisttable", method = RequestMethod.GET)
    public String testdf(HttpServletRequest request, Model model, HttpSession session,
                         @RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page,
                         @RequestParam(value = "baseKey", required = false) String baseKey,
                         @RequestParam(value = "type", required = false) String type,
                         @RequestParam(value = "status", required = false) String status,
                         @RequestParam(value = "time", required = false) String time,
                         @RequestParam(value = "icon", required = false) String icon) {
        attendceConFun.signsortpaging(request, model, session, page, baseKey, type, status, time, icon);
        return "attendce/attendcelisttable";
    }


    /**
     * 考勤 管理下级 员工考勤 记录 初始化
     *
     * @param request
     * @param session
     * @param page
     * @param baseKey
     * @param type
     * @param status
     * @param time
     * @param icon
     * @param model
     * @return
     */
    // 考勤管理某个管理员下面的所有员工的信息
    @RequestMapping("attendceatt")
    public String testdasf(HttpServletRequest request, HttpSession session,
                           @RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page,
                           @RequestParam(value = "baseKey", required = false) String baseKey,
                           @RequestParam(value = "type", required = false) String type,
                           @RequestParam(value = "status", required = false) String status,
                           @RequestParam(value = "time", required = false) String time,
                           @RequestParam(value = "icon", required = false) String icon, Model model) {
        attendceConFun.allsortpaging(request, session, page, baseKey, type, status, time, icon, model);
        return "attendce/attendceview";
    }

    /**
     * 考勤 记录 分页
     *
     * @param request
     * @param session
     * @param page
     * @param baseKey
     * @param type
     * @param status
     * @param time
     * @param icon
     * @param model
     * @return
     */
    // 考情管理分页
    @RequestMapping("attendcetable")
    public String table(HttpServletRequest request, HttpSession session,
                        @RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page,
                        @RequestParam(value = "baseKey", required = false) String baseKey,
                        @RequestParam(value = "type", required = false) String type,
                        @RequestParam(value = "status", required = false) String status,
                        @RequestParam(value = "time", required = false) String time,
                        @RequestParam(value = "icon", required = false) String icon, Model model) {
        attendceConFun.allsortpaging(request, session, page, baseKey, type, status, time, icon, model);
        return "attendce/attendcetable";
    }

    /**
     * 跳转 修改 页面 遍历 状态列表 and 类型列表
     * 单查 该数据 传 前台 展示
     *
     * @param aid
     * @param model
     * @param request
     * @return
     */
    // attendceedit
    @RequestMapping("/attendceedit")
    public String toUpdateAttendceedit(@RequestParam("aid") Long aid, Model model, HttpServletRequest request) {
        attendceConFun.typestatus(request);
        Attends attends = attenceDao.findOne(aid);
        model.addAttribute("attends", attends);
        return "attendce/attendceedit";
    }

    /**
     * 修改 考前管理 ，修改状态 and 备注
     *
     * @param req
     * @param model
     * @return
     */
    //    attendcesave
    @RequestMapping("/attendcesave")
    public String attendcesave(HttpServletRequest req, Model model) {
        Long id = Long.parseLong(req.getParameter("id"));
        System.out.println("id =" + id);
        // 根据 id 值 查 考勤修改前的值
        Attends attends = attenceDao.findOne(id);
        // 获取要修改 的值
        String remark = req.getParameter("remark").trim();
        Long status = Long.parseLong(req.getParameter("status").trim());
        // 直接将 获取的值来覆盖 修改前的属性值
        attends.setAttendsRemark(remark);
        attends.setStatusId(status);
        // 保存
        Attends save = attenceDao.save(attends);
        model.addAttribute("success", "修改成功");
        System.out.println("修改 后值打印 = " + save);
        return "forward:/attendceatt";
    }

    /**
     * 删除 下级 员工 考勤信息
     *
     * @param aid
     * @param model
     * @return
     */
    // attdelete
    @RequestMapping("/attdelete")
    public String attdelete(@RequestParam("aid") long aid, Model model) {
        System.out.println("aid =" + aid);
        Integer delete = attenceDao.delete(aid);
        if (delete > 0) {
            model.addAttribute("success", "删除成功!");
        } else {
            model.addAttribute("errormess", "删除失败!");
        }
        return "forward:/attendceatt";
    }
}
